//
//  SNHFeedbackGenerator.m
//  Pods
//
//  Created by 陈建蕾 on 2018/6/2.
//

#import "SNHFeedbackGenerator.h"
#import <UIKit/UIKit.h>

typedef NS_ENUM(NSUInteger, SNHFeedbackGeneratorType) {
    SNHFeedbackGeneratorLight,
    SNHFeedbackGeneratorMedium,
    SNHFeedbackGeneratorHeavy,
    SNHFeedbackGeneratorSelection,
    SNHFeedbackGeneratorSuccess,
    SNHFeedbackGeneratorWaring,
    SNHFeedbackGeneratorError
};

API_AVAILABLE(ios(10.0))
@interface SNHFeedbackGenerator ()
@property (nonatomic, strong) UIImpactFeedbackGenerator *lightGenerator;
@property (nonatomic, strong) UIImpactFeedbackGenerator *mediumGenerator;
@property (nonatomic, strong) UIImpactFeedbackGenerator *heavyGenerator;
@property (nonatomic, strong) UISelectionFeedbackGenerator *selectionGenerator;
@property (nonatomic, strong) UINotificationFeedbackGenerator *notificationGenerator;
@end

@implementation SNHFeedbackGenerator

+ (instancetype)shareInstance {
    static SNHFeedbackGenerator *generator = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        generator = [SNHFeedbackGenerator new];
    });
    return generator;
}

- (void)impactType:(SNHFeedbackGeneratorType)type {
    switch (type) {
        case SNHFeedbackGeneratorLight: {
            [self.lightGenerator prepare];
            [self.lightGenerator impactOccurred];
        } break;
            
        case SNHFeedbackGeneratorMedium: {
            [self.mediumGenerator prepare];
            [self.mediumGenerator impactOccurred];
        } break;
            
        case SNHFeedbackGeneratorHeavy: {
            [self.heavyGenerator prepare];
            [self.heavyGenerator impactOccurred];
        } break;
            
        case SNHFeedbackGeneratorSelection: {
            [self.selectionGenerator prepare];
            [self.selectionGenerator selectionChanged];
        } break;
            
        case SNHFeedbackGeneratorSuccess: {
            [self.notificationGenerator prepare];
            [self.notificationGenerator notificationOccurred:UINotificationFeedbackTypeSuccess];
        } break;
            
        case SNHFeedbackGeneratorWaring: {
            [self.notificationGenerator prepare];
            [self.notificationGenerator notificationOccurred:UINotificationFeedbackTypeWarning];
        } break;
            
        case SNHFeedbackGeneratorError: {
            [self.notificationGenerator prepare];
            [self.notificationGenerator notificationOccurred:UINotificationFeedbackTypeError];
        } break;
            
        default:
            break;
    }
}

+ (void)_impactType:(SNHFeedbackGeneratorType)type {
    if (@available(iOS 10.0, *)) {
        [[SNHFeedbackGenerator shareInstance] impactType:type];
    }
}

+ (void)light {
    [self _impactType:SNHFeedbackGeneratorLight];
}

+ (void)medium {
    [self _impactType:SNHFeedbackGeneratorMedium];
}

+ (void)heavy {
    [self _impactType:SNHFeedbackGeneratorHeavy];
}

+ (void)selection {
    [self _impactType:SNHFeedbackGeneratorSelection];
}

+ (void)success {
    [self _impactType:SNHFeedbackGeneratorSuccess];
}

+ (void)warning {
    [self _impactType:SNHFeedbackGeneratorWaring];
}

+ (void)error {
    [self _impactType:SNHFeedbackGeneratorError];
}

#pragma mark - Private
- (UIImpactFeedbackGenerator *)lightGenerator API_AVAILABLE(ios(10.0)) {
    if (!_lightGenerator) {
        _lightGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleLight];
    }
    return _lightGenerator;
}

- (UIImpactFeedbackGenerator *)mediumGenerator API_AVAILABLE(ios(10.0)) {
    if (!_mediumGenerator) {
        _mediumGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
    }
    return _mediumGenerator;
}

- (UIImpactFeedbackGenerator *)heavyGenerator API_AVAILABLE(ios(10.0)) {
    if (!_heavyGenerator) {
        _heavyGenerator = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleHeavy];
    }
    return _heavyGenerator;
}

- (UISelectionFeedbackGenerator *)selectionGenerator API_AVAILABLE(ios(10.0)) {
    if (!_selectionGenerator) {
        _selectionGenerator = [UISelectionFeedbackGenerator new];
    }
    return _selectionGenerator;
}

- (UINotificationFeedbackGenerator *)notificationGenerator API_AVAILABLE(ios(10.0)) {
    if (!_notificationGenerator) {
        _notificationGenerator = [UINotificationFeedbackGenerator new];
    }
    return _notificationGenerator;
}
@end
